package com.example.shortmessage.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

public abstract  class BaseFragment<T extends ViewBinding, V extends IBaseView<P>, P extends IBasePresenter> extends Fragment {
    //获取TAG的fragment名称
    public T mBinding;
    protected final String TAG = this.getClass().getSimpleName();
    protected P mPresenter;
    public Context context;

    @Override
    public void onAttach(Context ctx) {
        super.onAttach(ctx);
        context = ctx;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        attachPresenter();
        mBinding=initLayout(inflater, container);
        initView();
        return mBinding.getRoot();
    }

    /**
     * 添加Present相关
     */
    private void attachPresenter() {
        ((V) this).setPresenter(initPresenter());
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
    }

    /**
     * Present
     * @return
     */
    protected abstract P initPresenter();

    /**
     * 初始化布局
     *
     * @return 布局id
     */
    protected abstract T initLayout(LayoutInflater inflater,ViewGroup container);


    protected abstract void initView();



    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
